﻿namespace com.vs.General.BL
{
    using com.vs.General;
    using System;
    using System.Security.Cryptography;
    using System.Text;

    public class LicenseValidation
    {
        public string GetLicenseKey(Session session)
        {
            string[] files = null;
            string dir = null;
            string data = null;
            string str4 = null;
            string str5;
            try
            {
                dir = @"C:\Inetpub\wwwroot\SCV_APP\";
                files = FileManager.GetFiles(dir, LicenseKeyConstants.stLICENSE_FILE_EXT);
                if ((files == null) || (files.Length <= 0))
                {
                    return MessageManager.MSG_LIC_006;
                }
                if (files.Length > 1)
                {
                    return MessageManager.MSG_LIC_010;
                }
                data = "AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAn3kg12C680y3Dj/klFKlaQQAAAACAAAAAAADZgAAqAAAABAAAAAGRbkNmTQFuj5TRhsSAkc0AAAAAASAAACgAAAAEAAAAMGmKFDocJASKnWpQ+K/KYwQAAAAxiEjooJyk1iyj+oj6qZoaRQAAAAtEYU4JJhDWzzPNVKufM1WiqLzkg==";
                str4 = "AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAn3kg12C680y3Dj/klFKlaQQAAAACAAAAAAADZgAAqAAAABAAAABe7NOH8nd/+KASRg/xVw+xAAAAAASAAACgAAAAEAAAAMXC8Phjsy3+HSF9SHtZ+r4QAAAA+Zcbtu+Nc4FZpJhyrbzJYhQAAABrwTzMVXVGcuCDYxkfs3XMkOC5Ug==";
                ASCIIEncoding encoding = new ASCIIEncoding();
                MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
                byte[] bytes = encoding.GetBytes(EncryptionManager.GetDecryptedSecurityKey(data));
                byte[] rijnKey = provider.ComputeHash(bytes);
                byte[] buffer = encoding.GetBytes(EncryptionManager.GetDecryptedSecurityKey(str4));
                byte[] rijnIV = provider.ComputeHash(buffer);
                str5 = EncryptionManager.DecryptDataFromFile(files[0], rijnKey, rijnIV);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str5;
        }

        private string isValidBiosSerialNum(Session session, string stLicenseKey)
        {
            string str = null;
            LicenseKeyParser parser = new LicenseKeyParser();
            str = parser.rtrvBiosSerialNum(session, stLicenseKey).ToUpper();
            if (!(SystemManager.RtrvBiosSerialNum().ToUpper().Equals(str) || str.Equals(LicenseKeyConstants.stDEFAULT_BIOS_SERIAL_NUM)))
            {
                return MessageManager.MSG_LIC_005;
            }
            return null;
        }

        private string isValidExpirationDate(Session session, string stLicenseKey)
        {
            string b = null;
            LicenseKeyParser parser = new LicenseKeyParser();
            DateTime time = new DateTime();
            DateTime time2 = new DateTime();
            int num = 0;
            TimeSpan span = parser.rtrvExpirationDate(session, stLicenseKey).Subtract(DateTime.Now);
            time2 = parser.RtrvCreationDate(session, stLicenseKey);
            if ((span.TotalDays < 0.0) || (time2 > DateTime.Now))
            {
                b = parser.rtrvExpirationAction(session, stLicenseKey);
                if (StringManager.Equals(LicenseKeyConstants.stEXPIRATIONACTIONCONTINUE, b))
                {
                    return MessageManager.MSG_LIC_007;
                }
                if (StringManager.Equals(LicenseKeyConstants.stEXPIRATIONACTIONHALT, b))
                {
                    return MessageManager.MSG_LIC_008;
                }
            }
            if (span.TotalDays < 30.0)
            {
                num = (int) Math.Ceiling(span.TotalDays);
                return (MessageManager.MSG_LIC_009 + num);
            }
            return null;
        }

        private string isValidMachineName(Session session, string stLicenseKey)
        {
            string str = null;
            string str2 = null;
            LicenseKeyParser parser = new LicenseKeyParser();
            str = parser.rtrvMachineName(session, stLicenseKey).ToUpper();
            str2 = Environment.MachineName.ToUpper();
            if (!(str.Equals(LicenseKeyConstants.stDEFAULT_MACHINE_NAME) || str2.Equals(str)))
            {
                return MessageManager.MSG_LIC_005;
            }
            return null;
        }

        public string ValidateLicense(Session session)
        {
            string msg = null;
            string str = null;
            string str3;
            try
            {
                msg = this.GetLicenseKey(session);
                if (StringManager.IsErrorMessage(msg))
                {
                    return msg;
                }
                str = this.isValidMachineName(session, msg);
                if (!StringManager.IsEmpty(str))
                {
                    return str;
                }
                str = this.isValidBiosSerialNum(session, msg);
                if (!StringManager.IsEmpty(str))
                {
                    return str;
                }
                str = this.isValidExpirationDate(session, msg);
                if (!StringManager.IsEmpty(str))
                {
                    return str;
                }
                str3 = null;
            }
            catch (Exception exception)
            {
                throw exception;
            }
            return str3;
        }
    }
}

